package com.qf.controller;

import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wanghaotian
 * time:2022/12/16,0016 10:01
 * description:
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/admin/dashboard")
public class AdminDashboardController {

    @Autowired
    private DtsGoodsService goodsService;

    @Autowired
    private DtsUserService userService;

    @Autowired
    private DtsGoodsProductService productService;

    @Autowired
    private DtsOrderService orderService;

    @GetMapping("")
    public Object dashboard(){
        int goodsTotal = goodsService.count();
        int userTotal = userService.count();
        int productTotal = productService.count();
        int orderTotal = orderService.count();
        Map<String,Object> data = new HashMap<>();
        data.put("goodsTotal",goodsTotal);
        data.put("userTotal",userTotal);
        data.put("productTotal",productTotal);
        data.put("orderTotal",orderTotal);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/chart")
    public Object chart(){
        List<Map<String,Object>> categorySellData = orderService.findCategorySellData();
        List<Object> categoryNames = categorySellData.stream().map((category)->{
            return category.get("name");
        }).collect(Collectors.toList());
        Map<String,Object> categorySell = new HashMap<>();
        categorySell.put("categoryNames",categoryNames);
        categorySell.put("categorySellData",categorySellData);

        List<DayStatis> orderAmtsData = orderService.findOrderAmts();
        List<String> orderDayData = fetchDayData(orderAmtsData);
        List<BigDecimal> orderAmtData = new ArrayList<>();
        List<Integer> orderCntData = new ArrayList<>();
        for (String day : orderDayData) {
            for (DayStatis dayStatis : orderAmtsData) {
                if (dayStatis.getDayStr().equals(day)) {
                    orderAmtData.add(dayStatis.getAmts());
                    orderCntData.add(dayStatis.getCnts());
                    break;
                }
            }
        }
        Map<String,Object> orderAmts = new HashMap<>();
        orderAmts.put("dayData", orderDayData);
        orderAmts.put("orderAmtData",orderAmtData);
        orderAmts.put("orderCntData",orderCntData);

        List<DayStatis> userDayStatis = userService.findUserDayStatis();
        List<String> userDayData = fetchDayData(userDayStatis);
        List<String> userOrderDayData = unionDayData(orderDayData,userDayData);
        List<Integer> userCnt = new ArrayList<>();
        List<Integer> orderCnt = new ArrayList<>();
        a:for (String day : userOrderDayData) {
            for (DayStatis dayStatis : userDayStatis) {
                if (dayStatis.getDayStr().equals(day)) {
                    userCnt.add(dayStatis.getCnts());
                    continue a;
                }
            }
            userCnt.add(0);
        }
        a:for (String day : userOrderDayData) {
            for (DayStatis dayStatis : orderAmtsData) {
                if (dayStatis.getDayStr().equals(day)) {
                    orderCnt.add(dayStatis.getCnts());
                    continue a;
                }
            }
            orderCnt.add(0);
        }
        Map<String,Object> userOrderCnt = new HashMap<>();
        userOrderCnt.put("dayData", userDayData);
        userOrderCnt.put("userCnt", userCnt);
        userOrderCnt.put("orderCnt", orderCnt);

        Map<String,Object> data = new HashMap<>();
        data.put("categorySell",categorySell);
        data.put("orderAmts",orderAmts);
        data.put("userOrderCnt",userOrderCnt);
        return ResponseUtil.ok(data);
    }

    private List<String> unionDayData(List<String> orderDayData, List<String> userDayData) {
        Set<String> set = new HashSet<>();
        set.addAll(orderDayData);
        set.addAll(userDayData);
        List<String> list = new ArrayList<>();
        list.addAll(set);
        Collections.sort(list);
        return list;
    }

    private List<String> fetchDayData(List<DayStatis> dayStatis) {
        List<String> dayStrs = dayStatis.stream().map((day)->{
            return day.getDayStr();
        }).collect(Collectors.toList());
        Collections.sort(dayStrs);
        return dayStrs;
    }

}
